﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using Microsoft.SharePoint.Administration;
using Microsoft.Win32;
using System.IO;

namespace SPVisualDev.Services
{
    public class SharePointLibrary
    {
        public static bool CheckIf32bitOn64()
        {
            try
            {
                string path = (string)Registry.GetValue("HKEY_LOCAL_MACHINE\\Software\\Microsoft\\Shared Tools\\Web Server Extensions\\12.0", "Location", "");
                if (string.IsNullOrEmpty(path))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch { }

            return true;
        }

        public static bool CheckIfUserHasSuffAdminPrivOnFarm(bool bShowMessage)
        {
            try
            {
                SPFarm farm = SPFarm.Local;
                if (farm == null)
                    throw new ArgumentNullException();

                if (!farm.CurrentUserIsAdministrator())
                    throw new Exception();

                return true;
            }
            catch (ArgumentNullException)
            {
                if (bShowMessage)
                {
                    MessageBox.Show("This feature is not supported on 64 bit SharePoint installation.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return false;
                }
                else
                {
                    throw;
                }

            }
            catch
            {
                if (bShowMessage)
                {
                    if (Environment.OSVersion.Version.Major > 5)
                        MessageBox.Show("Current user doesn't have administrator privileges on the local sharepoint farm and / or no access to the configuration database. If the system has UAC turned on, please turn it off or try run this application as administrator.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    else
                        MessageBox.Show("Current user doesn't have administrator privileges on the local sharepoint farm and / or no access to the configuration database.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                return false;
            }
        }

        public static string GetSharePointInstallPath()
        {
            string path = null;

            //Try find the path by looking in the registry.
            try
            {
                path = (string)Registry.GetValue("HKEY_LOCAL_MACHINE\\Software\\Microsoft\\Shared Tools\\Web Server Extensions\\12.0", "Location", "");
            }
            catch { }

            //If it didn't work, try find a standard feature definition.
            try
            {
                if (!SharePointLibrary.CheckIf32bitOn64())
                {
                    if (string.IsNullOrEmpty(path))
                    {
                        SPFeatureDefinition def = SPFarm.Local.FeatureDefinitions["fields"];
                        DirectoryInfo info = Directory.GetParent(def.RootDirectory);
                        path = info.Parent.Parent.FullName;
                    }
                }
            }
            catch { }


            //Still no value? Try environment variables.
            try
            {
                if (string.IsNullOrEmpty(path))
                {
                    string probe_path = @"\Microsoft Shared\web server extensions\12\BIN\STSADM.exe";
                    path = Environment.GetFolderPath(Environment.SpecialFolder.CommonProgramFiles) + probe_path;
                    if (string.IsNullOrEmpty(path) || !File.Exists(path))
                        path = Environment.GetEnvironmentVariable("CommonProgramW6432") + @"\Microsoft Shared\web server extensions\12";
                    else
                        path = Environment.GetFolderPath(Environment.SpecialFolder.CommonProgramFiles) + @"\Microsoft Shared\web server extensions\12";
                }
            }
            catch { }

            //If the directory is not valid, throw an error.
            if (!Directory.Exists(path))
                throw new Exception("Can not find SharePoint 12 hive on system.");

            if (!string.IsNullOrEmpty(path))
                if (path.EndsWith("\\"))
                    path = path.Substring(0, path.Length - 1);

            return path;
        }

    }
}
